/* File			: mainNode.c													*/
/* Deskripsi	: Main program untuk mentest fungsi dan prosedur pada Node.c	*/
/* Isi			: program utama & semua nama lokal thd persoalan				*/

/* driver ADT Node */

/* ADT Node */

#include "Node.h"
#include "..\reader\Reader.h"

/* Bagian I : berisi kamus GLOBAL dan prototype */
/* deklarasi semua nama dan prosedur/fungsi global */
	#define ChoiceNumb 2 //number of choices available
	#define StrLength 255
	int ChoiceIdxMin = 0;
	int ChoiceIdxMax = ChoiceNumb;
	int ExitIdx = 0;

	//MAIN VARIABLES
	nodeList N;
	bool kluar = false;
	
	void CreateSeparator()
		{
			int i = 0;
			printf("\n");
			for (i; i<70; i++) printf("=");
			printf("\n\n");
		}
	
	void choices1();
	void ParseInput (int input); //Parsing Input
	char choices[ChoiceNumb][StrLength] = //choosen input
		{
			"Exit",
			"OPERASI(ChangeNodeTableAvailability, SearchNodeByName, AddNode, DestroyNode)"
		};
	
	//DO DEFAULT FUNCT
	void DoDefault();
	
	//TESTING FUNCT DEF
	void f1();
	
	void x0(bool *kluar);
	
	void x11();
	void x12();
	void x13();
	void x14();
	
	//MDALLOC
	void destroy();
	
	void CetaQ();

/* Bagian II : program utama */
	int mainLoop(); //main funct
	
	int main (int argc, char *argv[]) //main prog
		{
			/* Kamus lokal terhadap main */
			
			/* Algoritma */
				DoDefault();
				while (mainLoop());
			
			return 0;
		}
	
	int mainLoop()
		{
			int input;
			
			//Show Choices
			CreateSeparator();
			int i = ChoiceIdxMin;
			for(i; i< ChoiceNumb; i++) printf("[%d] %s \n", (i + ChoiceIdxMin), choices[i]);
			
			//Get Input
			while(1)
				{
					printf("Pilihan : "); scanf("%d", &input);
					input -= ChoiceIdxMin;
				
					//check for break :
					if ((input >= ChoiceIdxMin) && (input < ChoiceNumb)) break;
					printf("Pilihan Anda tidak valid! \n");
				}
			
			//Parse Input
				if (input == ExitIdx)
					{
						/* KAMUS LOKAL */
					
						/* ALGORITMA */
							destroy();
						
						return 0; //stop looping
					}
				ParseInput(input);
	
			return 1; //continue looping
		}

/* Bagian III : berisi realisasi kode program yang merupakan	*/
/* BODY dari semua prototype yg didefinisikan pada file ini		*/
/* yaitu pada bagian I, dengan urut-urutan yang sama			*/
	void choices1()
		{
			int choicezes;
			
			while(! kluar)
				{
					printf("[ 0] Selesai \n");
					printf("[ 1] ChangeNodeTableAvailability \n");
					printf("[ 2] SearchNodeByName \n");
					printf("[ 3] AddNode \n");
					printf("[ 4] DestroyNode \n");
					printf("Pilihan : "); scanf("%d", &choicezes);
					if (choicezes >= 0 && choicezes <= 4) break;
				}     
			switch (choicezes)
				{
					case 1 : x11(); break;
					case 2 : x12(); break;
					case 3 : x13(); break;
					case 4 : x14(); break;
					case 0 : x0(&kluar); break;
				}
			system("PAUSE");
		}

	// PARSING INPUT
		void ParseInput (int input)
			{
				switch(input)
					{
						case 1 : f1(); break;
					}
			}
	// DO DEFAULT FUNC
		void DoDefault()
			{
				N = CreateEmptyNodeList();
			}
	
	// TESTING FUNC REALIZATION
		void f1()
			{
				kluar = false;
				CreateSeparator();
				while(! kluar)
					{
						CreateSeparator();
						choices1();
					}
				//system("PAUSE");
			}

	void x0(bool *kluar) {(*kluar) = true;}
	
	void x11()
		{
			nodeAddress node; bool hasTMhs_baru, hasTMK_baru, hasTPMK_baru;
			
			CetaQ() ;
			printf("@[address] = "); scanf("%d", &node);
			printf("hasTMhs? [Y|y(1) / N|n(0)] "); scanf("%d", &hasTMhs_baru);
			printf("hasTMK? [Y|y(1) / N|n(0)] "); scanf("%d", &hasTMK_baru);
			printf("hasTPMK? [Y|y(1) / N|n(0)] "); scanf("%d", &hasTPMK_baru);
			ChangeNodeTableAvailability(node, hasTMhs_baru, hasTMK_baru, hasTPMK_baru);
			CetaQ();
		}
	void x12()
		{
			CetaQ();
			printf("Name : "); ReadArguments(NULL);
			printf("[%d]\n", SearchNodeByName(&N, CReader.argument[0]));
			CetaQ();
		}
	void x13()
		{
			float retrieveTime, updateTime, insertTime, deleteTime; bool hasClient, hasServer;
			nodeAddress P, Q = FIRST(N);
			bool Existance = false;
			
			CetaQ();
			printf("Info : \n");
			printf("Name : "); ReadArguments(NULL);
			printf("Retrieve Time = "); scanf ("%f", &retrieveTime);
			printf("Update Time = "); scanf ("%f", &updateTime);
			printf("Insert Time = "); scanf ("%f", &insertTime);
			printf("Delete Time = "); scanf ("%f", &deleteTime);
			printf("Client [Y|y(1) / N|n(0)] "); scanf ("%d", &hasClient);
			printf("Server [Y|y(1) / N|n(0)] "); scanf ("%d", &hasServer);
			P = AllocateNode(CReader.argument[0], retrieveTime, updateTime, insertTime, deleteTime, hasClient, hasServer);
			if (! ISLINKEDLISTEMPTY(N))
				{
					while (Q != NULL)
						{
							if (IsNodeEqual(P, Q))
								{
									Existance = true;
									break;
								}
							else
								{
									Q = NEXT(Q);
								}
						}
				}
			if (! Existance)
				{
					AddNode(&N, P);
				}
			else
				{
					printf("node sudah ada\n");
				}
			CetaQ();
		}
	void x14()
		{
			nodeAddress P;
			
			CetaQ();
			printf("@[address] = "); scanf("%d", &P);
			DestroyNode(&N, P);
			CetaQ();
		}

	void destroy()
		{
			nodeAddress Q;
			
			while (! ISLINKEDLISTEMPTY(N))
				{
					Q = FIRST(N);
					FIRST(N) = NEXT(Q);
					DeallocateNode(Q);
				}
			N = CreateEmptyNodeList();
		}

	void CetaQ()
		{
			nodeAddress Q = FIRST(N);
			
			if (Q == NULL)
				{
					printf("nodeList N kosong\n");
				}
			else
				{
					PrintNodeHeader();
					while (Q != NULL)
						{
							printf("[%d]:\n", Q);
							PrintNode(Q);
							if (HAS_TMHS(Q))
								{
									printf("-> TMhs\n");
								}
							if (HAS_TMK(Q))
								{
									printf("-> TMK\n");
								}
							if (HAS_TPMK(Q))
								{
									printf("-> TPMK\n");
								}
							Q = NEXT(Q);
						}
				}
		}
